package qc.module.duty.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import qc.common.core.unify.QCUnifyReturnValue;
import qc.module.duty.dto.team.DutyTeamMemberDto;
import qc.module.duty.entity.DutyTeamMember;
import qc.module.duty.mapper.DutyTeamMemberMapper;
import qc.module.duty.repository.DutyTeamMemberRepository;

import java.util.List;

/**
 * @projectName: qcdp
 * @package: qc.module.duty.service
 * @className: DutyTeamMemberService
 * @date: 2023/8/11
 * @version: 1.0
 */
@Service
public class DutyTeamMemberService {

    private DutyTeamMemberRepository dutyTeamMemberRepository;

    @Autowired
    public void setDutyTeamMemberRepository(DutyTeamMemberRepository dutyTeamMemberRepository) {
        this.dutyTeamMemberRepository = dutyTeamMemberRepository;
    }


    /**
     * 根据指定排班id查询值班排班人员信息，按照duty_team_member表中的odr升序排序
     *
     * @param id: 值班表id
     * @return DutyTeamMemberDto 返回信息
     * @author QcCheng Tech
     * @date 2023/8/10
     */
    public List<DutyTeamMemberDto> get(int id) {
        // 创建 LambdaQueryWrapper 对象，用于构建查询条件
        LambdaQueryWrapper<DutyTeamMember> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DutyTeamMember::getId, id);
        //按odr升序排序
        wrapper.orderByAsc(DutyTeamMember::getOdr);
        
        List<DutyTeamMember> dutyTeamMembers = dutyTeamMemberRepository.selectList(wrapper);
        // 如果查询结果为空，返回null
        if (dutyTeamMembers != null) {
            return DutyTeamMemberMapper.MAPPER.toTeamMemberDtoList(dutyTeamMembers);
        }
        return null;
    }

    /**
     * 值判断值班排班人员信息是否存在
     *
     * @param id:       值班表id
     * @param memberid: 人员id
     * @return Boolean 存在返回true
     * @author QcCheng Tech
     * @date 2023/8/10
     */
    boolean isExist(int id, int memberid) {
        // 创建查询条件包装器
        LambdaQueryWrapper<DutyTeamMember> wrapper = new LambdaQueryWrapper<>();
        // 设置等于条件，根据班值ID查询
        wrapper.eq(DutyTeamMember::getId, id);
        // 设置等于条件，根据人员ID查询
        wrapper.eq(DutyTeamMember::getMember, memberid);

        // 执行查询操作，返回查询结果
        DutyTeamMember dutyTeamMember = dutyTeamMemberRepository.selectOne(wrapper);

        // 判断查询结果是否存在
        if (dutyTeamMember != null) {
            return true;
        }
        return false;
    }

    /**
     * 更新值班排班人员信息
     *
     * @param dutyTeamMemberDto:
     * @return String
     * @author QcCheng Tech
     * @date 2023/8/11
     */
    public String update(DutyTeamMemberDto dutyTeamMemberDto) {
        DutyTeamMember en = DutyTeamMemberMapper.MAPPER.toEntity(dutyTeamMemberDto);

        // 创建更新条件包装器
        LambdaUpdateWrapper<DutyTeamMember> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(DutyTeamMember::getMember, en.getMember());
        updateWrapper.eq(DutyTeamMember::getId, en.getId());
        // 设置更新的字段和值
        updateWrapper.set(DutyTeamMember::getOdr, en.getOdr());
        updateWrapper.set(DutyTeamMember::getChief, en.getChief());

        // 执行更新操作，并判断是否更新成功
        if (dutyTeamMemberRepository.update(null, updateWrapper) < 1) {
            return QCUnifyReturnValue.Warn("修改失败");
        }

        return QCUnifyReturnValue.Success();
    }

    /**
     * 新增值班排班人员信息
     *
     * @param dutyTeamMemberDto:
     * @return String
     * @author QcCheng Tech
     * @date 2023/8/11
     */
    public String add(DutyTeamMemberDto dutyTeamMemberDto) {
        DutyTeamMember en = DutyTeamMemberMapper.MAPPER.toEntity(dutyTeamMemberDto);

        // 执行插入操作，并判断是否插入成功
        if (dutyTeamMemberRepository.insert(en) < 1) {
            return QCUnifyReturnValue.Warn("保存失败");
        }

        return QCUnifyReturnValue.Success();
    }

    /**
     * 删除值班排班人员信息
     *
     * @param dutyTeamMemberDto:
     * @return String
     * @author QcCheng Tech
     * @date 2023/8/11
     */
    public String delete(DutyTeamMemberDto dutyTeamMemberDto) {
        DutyTeamMember en = DutyTeamMemberMapper.MAPPER.toEntity(dutyTeamMemberDto);

        // 创建查询条件包装器
        LambdaQueryWrapper<DutyTeamMember> wrapper = new LambdaQueryWrapper<>();
        // 设置等于条件，根据班值ID查询
        wrapper.eq(DutyTeamMember::getId, en.getId());
        // 设置等于条件，根据人员ID查询
        wrapper.eq(DutyTeamMember::getMember, en.getMember());

        // 执行删除操作，并判断是否删除成功
        if (dutyTeamMemberRepository.delete(wrapper) < 1) {
            return QCUnifyReturnValue.Warn("保存失败");
        }

        return QCUnifyReturnValue.Success();
    }

}
    